package org.jproxy.json;

class TokenParser
{
	private StringBuilder content;
	private int current;
	private int size;

	TokenParser(StringBuilder content)
	{
		this.content = content;
		current = 0;
		size = content.length();
	}

	String nextToken()
	{
		int begin = current;
		for(; begin < size && !isValidChar(content.charAt(begin), true); begin++);
		int end =  begin + 1;
		if(begin < size && !isSpecialChar(content.charAt(begin)))
			for(; end < size && isValidChar(content.charAt(end), false); end++);
		current = end;
		return begin > size || end > size ? null : content.substring(begin, end);
	}

	private boolean isValidChar(char c, boolean appendSpecial)
	{
		boolean special = appendSpecial ? isSpecialChar(c) : false;
		return special || !(c < '0' || (c > '9' && c < 'A') || (c > 'Z' && c < 'a') || c > 'z');
	}

	private boolean isSpecialChar(char c)
	{
		return c == '{' || c == '}' || c == '[' || c == ']' || c == ',' || c == ':' || c == '"' || c == '\'';
	}

}
